package com.sky.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.constant.MessageConstant;
import com.sky.constant.StatusConstant;
import com.sky.context.BaseContext;
import com.sky.dto.CategoryDTO;
import com.sky.dto.CategoryPageQueryDTO;
import com.sky.entity.Category;
import com.sky.entity.Dish;
import com.sky.entity.Setmeal;
import com.sky.mapper.CategoryMapper;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.CategoryService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

/**
 * @Description TODO
 * @Author dingchunlin
 * @Date 2023/8/6 21:18
 * @Version 1.0.0
 */
@Service
public class CategoryServiceImpl implements CategoryService {

    @Autowired
    private CategoryMapper categoryMapper;

    @Autowired
    private DishMapper dishMapper;

    @Autowired
    private SetmealMapper setmealMapper;

    /**
     * 分类管理分页查询
     * @param categoryPageQueryDTO
     * @return
     */
    @Override
    public PageResult categoryByPage(CategoryPageQueryDTO categoryPageQueryDTO) {
        //创建一个返回类型
        PageResult result = new PageResult();
        PageHelper.startPage(categoryPageQueryDTO.getPage(),categoryPageQueryDTO.getPageSize());
        Page<Category> categoryPage = categoryMapper.categoryByPage(categoryPageQueryDTO);
        //将分页数据封装到返回类型中
        result.setTotal(categoryPage.getTotal());
        result.setRecords(categoryPage.getResult());
        //将结果进行返回
        return result;
    }

    /**
     *添加分类
     * @param categoryDTO
     * @return
     */
    @Override
    public void saveCategory(CategoryDTO categoryDTO) {
        Category category = new Category();
        //将DTO中的复制到category中
        BeanUtils.copyProperties(categoryDTO,category);
        //设置状态
        category.setStatus(StatusConstant.ENABLE);
        //将数据进行保存
        categoryMapper.saveCategory(category);
    }

    /**
     * 根据分类id进行修改
     * @param categoryDTO
     * @return
     */
    @Override
    public void updateCategory(CategoryDTO categoryDTO) {
        Category category = new Category();
        //将DTO中的复制到category中
        BeanUtils.copyProperties(categoryDTO,category);
        //设置顺序
        category.setSort(category.getSort());
        //将数据进行处理
        categoryMapper.updateCategory(category);
    }

    /**
     * 根据分类id进行状态的修改
     * @param status
     * @param id
     * @return
     */
    @Override
    public void updateStatus(Integer status, Long id) {
        Category category = new Category();
        //设置状态
        category.setStatus(status);
        //设置id
        category.setId(id);
        //将数据进行处理
        categoryMapper.updateStatus(category);
    }

    /**
     * 根据分类id进行删除
     * @param id
     * @return
     */
    @Override
    public Result deleteCategory(Long id) {
        List<Dish> dishes = dishMapper.selectByCategoryId(id);
        List<Setmeal> setmeals = setmealMapper.selectByCategoryId(id);
        if (dishes.size()>0) {
            //看当前分类是否关联了菜品
            return Result.error(MessageConstant.CATEGORY_BE_RELATED_BY_DISH);
        }else if (setmeals.size()>0){
            //看当前分类是否关联了套餐
            return Result.error(MessageConstant.CATEGORY_BE_RELATED_BY_SETMEAL);
        }
        //进行删除
        categoryMapper.deleteCategory(id);
        return Result.success();
    }

    /**
     * 根据类型查询分类
     * @param type
     * @return
     */
    @Override
    public List<Category> categoryList(Integer type) {
        return categoryMapper.categoryList(type);
    }
}
